package com.tinyengine.it.common.utils;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.tinyengine.it.common.enums.AiType;
import lombok.extern.slf4j.Slf4j;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class DeepseekUtils {

    private static final String apiKey = "cKRaLTBlW5AOvE03YBNyR2vs6Nt70Rse-XpXwDUyAbM0xrMoZznJaRmj__ITQd7nXrtP6ELQU5xFeThr2n8TGQ";

    private static final String commonStr = "将这个以逗号分割的字符串拆分 ";
    private static final String commonStr_ = "多个单词以下划线分割 ";
    private static final String commonStrHump = "多个单词以驼峰命名 ";
    private static final String oneClickGeneration = "， 转换为下面格式：[{ 'key': 'formdata', 'fieldType': 'String', 'description': 'haha'}] ，key字段为翻译的英文 %s fieldType为java的字段类型，description 为字段中文说明，其他字段保持不变，直接返回数据，不要有其他说明信息";

    public static com.alibaba.fastjson2.JSONArray DeepseekAi(String params, AiType aiType) {
        // API地址和 API Key
        String url = "https://maas-cn-southwest-2.modelarts-maas.com/v1/infers/271c9332-4aa6-4ff5-95b3-0cf8bd94c394/v1/chat/completions";
        String param = commonStr + "\"" + params + "\"";
        switch (aiType) {
            case 驼峰:
                param += String.format(oneClickGeneration, commonStrHump);
                break;
            case 下划线:
                param += String.format(oneClickGeneration, commonStr_);
                break;
        }
        log.info("请求参数：{}", param);
        JSONArray array = JSONUtil.createArray();
        array.add(JSONUtil.createObj()
                .put("role", "system")
                .put("content", "你好"));
        array.add(JSONUtil.createObj()
                .put("role", "user")
                .put("content", param));
        // 请求体
        String data = JSONUtil.toJsonStr(
                JSONUtil.createObj()
                        .put("model", "DeepSeek-V3")
                        .put("max_tokens", 1024)
                        .put("messages", array

                        )
                        .put("stream", false)
                        .put("temperature", 0.6)
        );

        // 发送 POST 请求
        String response = HttpUtil.createPost(url)
                .header("Authorization", "Bearer " + apiKey)
                .header("Content-Type", "application/json")
                .body(data)
                .execute()
                .body();

        // 打印返回结果

        JSONObject jsonObject = JSON.parseObject(response);

        // 提取 content 字符串
        String contentStr = jsonObject.getJSONArray("choices")
                .getJSONObject(0)
                .getJSONObject("message")
                .getString("content");

        log.info("AI生成结果：{}", contentStr);

        Pattern pattern = Pattern.compile("\\[.*]", Pattern.DOTALL);
        Matcher matcher = pattern.matcher(contentStr);

        if (matcher.find()) {
            String group = matcher.group();
            return JSON.parseArray(group);
        }
        return new com.alibaba.fastjson2.JSONArray();
        // 解析 content 字符串为 JSON 数组
        //com.alibaba.fastjson2.JSONArray contentArray = JSON.parseArray(contentStr);

    }

}
